package org.nowcoder.leetcode.BTree;

/**
 * Title  : 101. Symmetric Tree
 * Source : https://leetcode.cn/problems/symmetric-tree/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-08-30
 */

public class LeetCode_101 {
    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private boolean check(TreeNode l, TreeNode r) {
        //递归终止条件
        if (l == null && r == null) {
            return true;
        }
        if (l == null || r == null) {
            return false;
        }
        if (l.val != r.val) {
            return false;
        }
        return check(l.left, r.right) && check(l.right, r.left);
    }

    public boolean isSymmetric(TreeNode root) {
        //递归实现
        return check(root.left, root.right);
    }
}
